package com.example.qiangdan.redisson;

import org.redisson.api.*;

import java.util.concurrent.TimeUnit;

/**
 * @author
 */
public class RedissonLockerClient {

	private RedissonClient client;

    public RLock getLock(String lockKey) {
    	return client.getLock(lockKey);
    }
    
    public RLock lock(String lockKey) {
        RLock lock = client.getLock(lockKey);
        lock.lock();
        return lock;
    }

   
    public RLock lock(String lockKey, int leaseTime) {
        RLock lock = client.getLock(lockKey);
        lock.lock(leaseTime, TimeUnit.SECONDS);
        return lock;
    }
    
   
    public RLock lock(String lockKey, TimeUnit unit ,int timeout) {
        RLock lock = client.getLock(lockKey);
        lock.lock(timeout, unit);
        return lock;
    }
    
   
    public boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
        RLock lock = client.getLock(lockKey);
        try {
            return lock.tryLock(waitTime, leaseTime, unit);
        } catch (InterruptedException e) {
            return false;
        }
    }
    
    public boolean tryLock(String lockKey) {
    	RLock lock = client.getLock(lockKey);
    	return lock.tryLock();
    }
    
   
    public void unlock(String lockKey) {
        RLock lock = client.getLock(lockKey);
        lock.unlock();
    }
    
   
    public void unlock(RLock lock) {
        lock.unlock();
    }
    
    public RMap<String, Object> getRmap(final String mapName) {
		return client.getMap(mapName);
	}

	public RMapCache<String, Object> getRMapCache(final String mapName) {
		return client.getMapCache(mapName);
	}

	public <T> RList<T> getRList(final String listName,Class<T> clz) {
		return client.getList(listName);
	}
	
    public void setRedissonClient(RedissonClient client) {
        this.client = client;
    }

}
